<%-- 
    Document   : BidScreen
    Created on : Jul 21, 2013, 7:49:15 PM
    Author     : Ndnam
--%>

<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="classes.Bid"%>
<%@page import="classes.Auction"%>
<%@page import="classes.User"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="auctionDAO" class="classes.AuctionDAO"/>

<%
    //If user is not logged in, go back to home
    User user = (User) session.getAttribute("user");
    if (user == null) {
        RequestDispatcher dispatch = request.getRequestDispatcher("Home.jsp");
        dispatch.forward(request, response);
    }
    String amount = request.getParameter("bidamount");
    if (amount != null) {
        int bidAmount = Integer.parseInt(amount);
        Auction auction = (Auction) session.getAttribute("auction");
        if (bidAmount > auction.getLastPrice()) {
            auction.setLastPrice(bidAmount);
            auction.setLastbidder(user.getUserID());
            auctionDAO.updateAuction(auction);

            //------> ADD TIME CHECKING LOGIC HERE <-------

            //Get current time
            SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
            Date now = new Date();
            String strDate = sdfDate.format(now);
            Bid newBid = new Bid(0, auction.getAuctionID(), user.getUserID(), bidAmount, "Winning", strDate);
            auctionDAO.insertBid(newBid);
        } else {
            out.println("Bid amount must be larger than current bid");
        }
    }
%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Auction</title>
    </head>
    <body>
        <%
            Auction auction;
            String id = request.getParameter("auction");
            if (id == null) {
                auction = (Auction) session.getAttribute("auction");
            } else {
                int auctionID = Integer.parseInt(id);
                auction = auctionDAO.searchAuctionByID(auctionID);
                session.setAttribute("auction", auction);
            }
            if (auction == null) {
                out.println("Auction not found");
            } else {
                out.println("Seller: " + auctionDAO.searchUserByID(auction.getCreator()).getUsername() + "<br>");
                out.println("Product name: " + auction.getProductName() + "<br>");
                out.println("Initial price: " + auction.getInitialPrice() + "<br>");
                out.println("Current bid: " + auction.getLastPrice() + "<br>");
                out.println("Begin time: " + auction.getBeginDate() + " " + auction.getBeginTime() + "<br>");
                out.println("End time: " + auction.getEndDate() + " " + auction.getEndTime() + "<br>");
                out.println("<form><input type=\"text\" name=\"bidamount\"><input type=\"submit\" value=\"Place Bid\"></form>");
            }
        %>
    </body>
</html>
